#pragma once

#include <optional>
#include <QString>
#include "../nlohmann/json.hpp"

using json = nlohmann::json;

class Device {
public:
    std::optional<int> deviceId;
    std::optional<std::string> name;
    std::optional<std::string> type;
    std::optional<std::string> location;
    std::optional<std::string> manufacturer;
    std::optional<std::string> model;
    std::optional<std::string> status;
    std::optional<std::string> installationDate;

    json toJson() const {
        json j;
        j["deviceId"] = deviceId ? json(*deviceId) : json(nullptr);
        j["name"] = name ? json(*name) : json(nullptr);
        j["type"] = type ? json(*type) : json(nullptr);
        j["location"] = location ? json(*location) : json(nullptr);
        j["manufacturer"] = manufacturer ? json(*manufacturer) : json(nullptr);
        j["model"] = model ? json(*model) : json(nullptr);
        j["status"] = status ? json(*status) : json(nullptr);
        j["installationDate"] = installationDate ? json(*installationDate) : json(nullptr);
        return j;
    }

    static Device fromJson(const json &j) {
        Device d;
        if (j.contains("deviceId") && !j["deviceId"].is_null()) d.deviceId = j["deviceId"].get<int>();
        if (j.contains("name") && !j["name"].is_null()) d.name = j["name"].get<std::string>();
        if (j.contains("type") && !j["type"].is_null()) d.type = j["type"].get<std::string>();
        if (j.contains("location") && !j["location"].is_null()) d.location = j["location"].get<std::string>();
        if (j.contains("manufacturer") && !j["manufacturer"].is_null())
            d.manufacturer = j["manufacturer"].get<std::string>();
        if (j.contains("model") && !j["model"].is_null()) d.model = j["model"].get<std::string>();
        if (j.contains("status") && !j["status"].is_null()) d.status = j["status"].get<std::string>();
        if (j.contains("installationDate") && !j["installationDate"].is_null())
            d.installationDate = j["installationDate"].get<std::string>();
        return d;
    }
};

